import io, re
p = r'static/js/admin.js'
s = io.open(p, 'r', encoding='utf-8').read()
orig = s
# 1) data(): add jsonModal after showOrdersRaw
s = s.replace('showOrdersRaw: false,', 'showOrdersRaw: false,\n      jsonModal: { visible: false, title: "", pretty: "" },')
# 2) methods: add openOrderJson / closeJsonModal after handleOrdersPageSizeChange
s = s.replace('handleOrdersPageSizeChange() {\n      this.ordersPagination.pageIndex = 1;\n      this.fetchOrders();\n    },',
              'handleOrdersPageSizeChange() {\n      this.ordersPagination.pageIndex = 1;\n      this.fetchOrders();\n    },\n    openOrderJson(order) {\n      try {\n        let raw = null;\n        if (this.ordersRaw && Array.isArray(this.ordersRaw.recordList)) {\n          raw = this.ordersRaw.recordList.find(r => String(r.code) === String(order.code)) || null;\n        }\n        const payload = raw || order || {};\n        this.jsonModal.title = `订单 JSON - ${order && order.code ? order.code : ''}`.trim();\n        this.jsonModal.pretty = JSON.stringify(payload, null, 2);\n        this.jsonModal.visible = true;\n      } catch (e) {\n        this.jsonModal.title = '订单 JSON';\n        this.jsonModal.pretty = String(e);\n        this.jsonModal.visible = true;\n      }\n    },\n    closeJsonModal() {\n      this.jsonModal.visible = false;\n    },')
# 3) template: add 操作列 header and row button
m = re.search(r"template:\s*`([\s\S]*?)`", s)
if m:
    tpl = m.group(1)
    # add header
    tpl = tpl.replace('<th>计划完工</th>\n            </tr>', '<th>计划完工</th>\n              <th>操作</th>\n            </tr>')
    # add row button after plan date cell
    tpl = tpl.replace("{{ formatDate(order.planDate) }}</td>\n            </tr>", "{{ formatDate(order.planDate) }}</td>\n            <td><button class=\"btn secondary\" type=\"button\" @click=\"openOrderJson(order)\">JSON</button></td>\n            </tr>")
    # add modal overlay near the end of orders section (after our previous extra or before </section>)
    anchor = tpl.find('查看原始标准结构')
    insert_at = None
    if anchor != -1:
        # find end of that line
        insert_at = anchor
        # move to after the block by searching next '</section>' and insert before it
        sec_end = tpl.find('</section>', anchor)
        if sec_end != -1:
            insert_at = sec_end
    else:
        # fallback: insert before first closing orders section
        orders_start = tpl.find("<section v-if=\"activeTab === 'orders'\"")
        if orders_start != -1:
            sec_end = tpl.find('</section>', orders_start)
            if sec_end != -1:
                insert_at = sec_end
    if insert_at is not None:
        modal = "\n      <div v-if=\"jsonModal.visible\" class=\"modal-mask\" style=\"position:fixed;left:0;top:0;right:0;bottom:0;background:rgba(0,0,0,0.35);display:flex;align-items:center;justify-content:center;z-index:9999;\">\n        <div class=\"modal\" style=\"background:#fff;border-radius:8px;min-width:60vw;max-width:80vw;max-height:80vh;display:flex;flex-direction:column;box-shadow:0 8px 24px rgba(0,0,0,0.2);\">\n          <div style=\"display:flex;align-items:center;justify-content:space-between;padding:12px 16px;border-bottom:1px solid #eee;\">\n            <h3 style=\"margin:0;font-size:16px;\">{{ jsonModal.title }}</h3>\n            <button class=\"btn\" type=\"button\" @click=\"closeJsonModal\">关闭</button>\n          </div>\n          <div style=\"padding:12px;overflow:auto;\">\n            <pre style=\"margin:0;white-space:pre-wrap;word-break:break-all;\">{{ jsonModal.pretty }}</pre>\n          </div>\n        </div>\n      </div>\n"
        tpl = tpl[:insert_at] + modal + tpl[insert_at:]
    s = s[:m.start(1)] + tpl + s[m.end(1):]

if s != orig:
    io.open(p, 'w', encoding='utf-8', newline='\n').write(s)
    print('UPDATED')
else:
    print('NOCHANGE')
